package com.ruoyi.system.service.award.research.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.award.research.ResearchAwardInfo;
import com.ruoyi.system.domain.award.research.ResearchAwardTeacher;
import com.ruoyi.system.mapper.award.research.ResearchAwardInfoMapper;
import com.ruoyi.system.mapper.award.research.ResearchAwardTeacherMapper;
import com.ruoyi.system.service.award.research.IResearchAwardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 科研获奖信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-27
 */
@Service
public class ResearchAwardInfoServiceImpl implements IResearchAwardInfoService
{
    @Autowired
    private ResearchAwardInfoMapper researchAwardInfoMapper;
    @Autowired
    private ResearchAwardTeacherMapper researchAwardTeacherMapper;
    /**
     * 查询科研获奖信息
     * 
     * @param id 科研获奖信息主键
     * @return 科研获奖信息
     */
    @Override
    public ResearchAwardInfo selectResearchAwardInfoById(Long id)
    {
        ResearchAwardInfo researchAwardInfo = researchAwardInfoMapper.selectResearchAwardInfoById(id);
        ResearchAwardTeacher ra = new ResearchAwardTeacher();
        ra.setResearchId(id);
        List<ResearchAwardTeacher> teachers = researchAwardTeacherMapper.selectResearchAwardTeacherList(ra);
        researchAwardInfo.setTeachers(teachers);

        return researchAwardInfo;
    }

    /**
     * 查询科研获奖信息列表
     * 
     * @param researchAwardInfo 科研获奖信息
     * @return 科研获奖信息
     */
    @Override
    public List<ResearchAwardInfo> selectResearchAwardInfoList(ResearchAwardInfo researchAwardInfo)
    {
        //如果是老师，则只显示自己上传的
        if(researchAwardInfo.getCurrentRole()!=null&&researchAwardInfo.getCurrentRole().equals("teacher")){
            researchAwardInfo.setUserId(getUserId());
        }
        //如果是部门，则显示老师是系里的所有
        if(researchAwardInfo.getCurrentRole()!=null&&researchAwardInfo.getCurrentRole().equals("department")){
            researchAwardInfo.setDeptId(SecurityUtils.getDeptId());
        }
        List<ResearchAwardInfo> list = researchAwardInfoMapper.selectResearchAwardInfoList(researchAwardInfo);
        for(ResearchAwardInfo rb : list){
            ResearchAwardTeacher ra = new ResearchAwardTeacher();
            ra.setResearchId(rb.getId());
            List<ResearchAwardTeacher> teachers = researchAwardTeacherMapper.selectResearchAwardTeacherList(ra);
            rb.setTeachers(teachers);
        }
        return list;
    }

    /**
     * 新增科研获奖信息
     * 
     * @param researchAwardInfo 科研获奖信息
     * @return 结果
     */
    @Override
    public int insertResearchAwardInfo(ResearchAwardInfo researchAwardInfo)
    {
        if(researchAwardInfo.getId()!=null){
            ResearchAwardTeacher ra = new ResearchAwardTeacher();
            ra.setResearchId(researchAwardInfo.getId());
            ra.setRank(researchAwardInfo.getRank());
            ra.setUserId(String.valueOf(researchAwardInfo.getUserId()));
            ra.setUserName(researchAwardInfo.getUserName());
            researchAwardTeacherMapper.insertResearchAwardTeacher(ra);
        }else{
            int result = researchAwardInfoMapper.insertResearchAwardInfo(researchAwardInfo);
            if (result == 1) {
                // 插入成功，返回主键ID
                Long id = researchAwardInfo.getId();
                ResearchAwardTeacher ra = new ResearchAwardTeacher();
                ra.setResearchId(id);
                ra.setRank(researchAwardInfo.getRank());
                ra.setUserId(String.valueOf(researchAwardInfo.getUserId()));
                ra.setUserName(researchAwardInfo.getUserName());
                researchAwardTeacherMapper.insertResearchAwardTeacher(ra);
            }
        }
        return 1;
    }

    /**
     * 修改科研获奖信息
     * 
     * @param researchAwardInfo 科研获奖信息
     * @return 结果
     */
    @Override
    public int updateResearchAwardInfo(ResearchAwardInfo researchAwardInfo)
    {
        return researchAwardInfoMapper.updateResearchAwardInfo(researchAwardInfo);
    }

    /**
     * 批量删除科研获奖信息
     * 
     * @param ids 需要删除的科研获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteResearchAwardInfoByIds(Long[] ids)
    {
        for(Long id : ids){
            ResearchAwardTeacher ra = new ResearchAwardTeacher();
            ra.setResearchId(id);
            List<ResearchAwardTeacher> teachers = researchAwardTeacherMapper.selectResearchAwardTeacherList(ra);
            if(!teachers.isEmpty()){
                for (ResearchAwardTeacher teacher : teachers) {
                    researchAwardTeacherMapper.deleteResearchAwardTeacherById(teacher.getId());
                }
            }
        }
        return researchAwardInfoMapper.deleteResearchAwardInfoByIds(ids);
    }

    /**
     * 删除科研获奖信息信息
     * 
     * @param id 科研获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteResearchAwardInfoById(Long id)
    {
        return researchAwardInfoMapper.deleteResearchAwardInfoById(id);
    }

    /**
     * 获取科研获奖信息
     * @param data
     * @return
     */
    @Override
    public ResearchAwardInfo getAwardInfoByKeyword(String data) {
        return researchAwardInfoMapper.getAwardInfoByKeyword( data);
    }
}
